package cn.ibizlab.odoo.core.odoo_mail.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Imail_thread;
import cn.ibizlab.odoo.core.odoo_mail.domain.Mail_thread;
import cn.ibizlab.odoo.core.odoo_mail.filter.Mail_threadSearchContext;
import cn.ibizlab.odoo.core.odoo_mail.service.IMail_threadService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[EMail线程] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Mail_threadServiceImpl implements IMail_threadService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Imail_threadClientService mail_threadClientService;

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Imail_thread clientModel = mail_threadClientService.createModel();
        clientModel.setId(id);
        mail_threadClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean create(Mail_thread mail_thread) {
        boolean bOk = false ;
        Imail_thread clientModel = this.convert2Model(mail_thread,null);
        mail_threadClientService.create(clientModel) ;
        this.convert2Domain(clientModel,mail_thread);
        bOk = true ;

        return bOk ;
    }

    @Override
    public Mail_thread get(Integer id) {
        Imail_thread clientModel = mail_threadClientService.createModel();
        clientModel.setId(id);
        mail_threadClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Mail_thread get(Mail_thread mail_thread) {
        Mail_thread retmail_thread = this.get(mail_thread.getId()) ;
    	BeanCopier copier=BeanCopier.create(retmail_thread.getClass(), mail_thread.getClass(), false);
        copier.copy(retmail_thread, mail_thread, null);
        return mail_thread ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //mail_threadClientService.removeBatch(mail_threads) ;

    }

    @Override
    public void createBatch(List<Mail_thread> mail_threads, int batchSize) {

                        //mail_threadClientService.createBatch(mail_threads,batchSize) ;

    }

    @Override
    public void updateBatch(List<Mail_thread> mail_threads, int batchSize) {

        //mail_threadClientService.updateBatch(mail_threads,batchSize) ;

    }

    @Override
    public boolean update(Mail_thread mail_thread) {
        boolean bOk = false ;
        Imail_thread clientModel = this.convert2Model(mail_thread,null);
        mail_threadClientService.update(clientModel) ;
        this.convert2Domain(clientModel,mail_thread) ;
        bOk = true;
        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Mail_thread> searchDefault(Mail_threadSearchContext context) {
        List<Mail_thread> list = new ArrayList<Mail_thread>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_thread> clientModelList = mail_threadClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_thread clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_thread>(list, context.getPageable(), clientModelList.getTotalElements());
    }




    public Imail_thread convert2Model(Mail_thread domain , Imail_thread model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = mail_threadClientService.createModel() ;
        }

         if(domain.getMessage_unread_counterDirtyFlag())
            model.setMessage_unread_counter(domain.getMessage_unread_counter());
         if(domain.getMessage_main_attachment_idDirtyFlag())
            model.setMessage_main_attachment_id(domain.getMessage_main_attachment_id());
         if(domain.getMessage_needactionDirtyFlag())
            model.setMessage_needaction(domain.getMessage_needaction());
         if(domain.getMessage_attachment_countDirtyFlag())
            model.setMessage_attachment_count(domain.getMessage_attachment_count());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getMessage_has_errorDirtyFlag())
            model.setMessage_has_error(domain.getMessage_has_error());
         if(domain.getMessage_channel_idsDirtyFlag())
            model.setMessage_channel_ids(domain.getMessage_channel_ids());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getWebsite_message_idsDirtyFlag())
            model.setWebsite_message_ids(domain.getWebsite_message_ids());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getMessage_needaction_counterDirtyFlag())
            model.setMessage_needaction_counter(domain.getMessage_needaction_counter());
         if(domain.getMessage_follower_idsDirtyFlag())
            model.setMessage_follower_ids(domain.getMessage_follower_ids());
         if(domain.getMessage_is_followerDirtyFlag())
            model.setMessage_is_follower(domain.getMessage_is_follower());
         if(domain.getMessage_partner_idsDirtyFlag())
            model.setMessage_partner_ids(domain.getMessage_partner_ids());
         if(domain.getMessage_idsDirtyFlag())
            model.setMessage_ids(domain.getMessage_ids());
         if(domain.getMessage_has_error_counterDirtyFlag())
            model.setMessage_has_error_counter(domain.getMessage_has_error_counter());
         if(domain.getMessage_unreadDirtyFlag())
            model.setMessage_unread(domain.getMessage_unread());
        return model ;
    }

    public Mail_thread convert2Domain( Imail_thread model ,Mail_thread domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Mail_thread();
        }

        if(model.getMessage_unread_counterDirtyFlag())
            domain.setMessage_unread_counter(model.getMessage_unread_counter());
        if(model.getMessage_main_attachment_idDirtyFlag())
            domain.setMessage_main_attachment_id(model.getMessage_main_attachment_id());
        if(model.getMessage_needactionDirtyFlag())
            domain.setMessage_needaction(model.getMessage_needaction());
        if(model.getMessage_attachment_countDirtyFlag())
            domain.setMessage_attachment_count(model.getMessage_attachment_count());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getMessage_has_errorDirtyFlag())
            domain.setMessage_has_error(model.getMessage_has_error());
        if(model.getMessage_channel_idsDirtyFlag())
            domain.setMessage_channel_ids(model.getMessage_channel_ids());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getWebsite_message_idsDirtyFlag())
            domain.setWebsite_message_ids(model.getWebsite_message_ids());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getMessage_needaction_counterDirtyFlag())
            domain.setMessage_needaction_counter(model.getMessage_needaction_counter());
        if(model.getMessage_follower_idsDirtyFlag())
            domain.setMessage_follower_ids(model.getMessage_follower_ids());
        if(model.getMessage_is_followerDirtyFlag())
            domain.setMessage_is_follower(model.getMessage_is_follower());
        if(model.getMessage_partner_idsDirtyFlag())
            domain.setMessage_partner_ids(model.getMessage_partner_ids());
        if(model.getMessage_idsDirtyFlag())
            domain.setMessage_ids(model.getMessage_ids());
        if(model.getMessage_has_error_counterDirtyFlag())
            domain.setMessage_has_error_counter(model.getMessage_has_error_counter());
        if(model.getMessage_unreadDirtyFlag())
            domain.setMessage_unread(model.getMessage_unread());
        return domain ;
    }


}
